<?php
namespace Ucmaim\Model;
use Think\Model;


class MemberModel extends Model{
    // 获取用户列表
    public function get(){
        return $this->select();
    }

    // 根据用户ID获取用户信息
    public function memberInfo($uid){
        if(! is_numeric($uid)) return [];
        $map['member_id'] = $uid;
        return $this->where($map)->find();
    }

    // 添加会员
    public function addUser($data){
        if($this->create($data)){
            $this->add();
            return true;
        }
        return false;
    }

    // 更改会员信息
    public function updateUser($uid, $data){
        $map['member_id'] = $uid;
        return $this->where($map)->save($data);
    }

    // 更改会员状态
    public function updateMemberStatus($uid, $status){
        $data['member_status'] = $status;
        $map['member_id'] = $uid;
        return $this->where($map)->save($data);
    }

    // 检查某个字段是否已存在
    public function isExist($field, $search, $except = 0){
        if( empty($search) ) return 0;
        $map[$field] = $search;
        if( $except !== 0 ){
            $map['member_id'] = array('NEQ', $except);
        }
        $result = $this->where($map)->find();
        return empty($result) ? 0 : 1;
    }

    // 通过手机号码更改用户密码
    public function updateMemberPassByMobile($mobile, $password, $md5 = true){
        $map['member_mobile'] = $mobile;
        $data['member_password'] = true === $md5 ? md5(trim($password)) : trim($password);
        return $this->where($map)->save($data);
    }
}